package cn.com.surker.production.service.impl;

import cn.com.surker.common.MessageCenterException;
import cn.com.surker.enums.production.CountBillTypeEnum;
import cn.com.surker.enums.production.ProductionContractTypeEnum;
import cn.com.surker.enums.production.car.CarModelTypeEnum;
import cn.com.surker.enums.production.car.TransportTypeEnum;
import cn.com.surker.enums.sale.ContractCategoryEnum;
import cn.com.surker.oa.dao.OaProductionContractDao;
import cn.com.surker.oa.vo.ProductionContractVo;
import cn.com.surker.production.entity.ProductionTransportPrice;
import cn.com.surker.production.dao.ProductionTransportPriceDao;
import cn.com.surker.production.service.TranPriceService;
import cn.com.surker.sale.dao.SaleContractDao;
import cn.com.surker.sale.entity.SaleContract;
import cn.com.surker.system.dao.SysMessageRecordDao;
import cn.com.surker.system.dao.SysUserPositionDao;
import cn.com.surker.system.entity.SysMessageRecord;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.sale.SaleContractVo;
import cn.com.surker.wechat.WechatOaGroupApi;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.surker.spring.RestResultDto;

import javax.annotation.Resource;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
* <p>
    * 生产运输单价表 服务实现类
    * </p>
*
* @author admin
* @since 2024-12-05
*/
public abstract class ProductionTransportPriceServiceImpl extends ServiceImpl<ProductionTransportPriceDao, ProductionTransportPrice> implements TranPriceService {

    @Resource
    protected ProductionTransportPriceDao transportPriceDao;
    @Resource
    protected OaProductionContractDao productionContractDao;
    @Resource
    protected SysMessageRecordDao messageRecordDao;
    @Resource
    protected SysUserPositionDao userPositionDao;
    @Resource
    protected SaleContractDao saleContractDao;

    /**
     * 查询生产合同运输单价信息
     * @param contractId
     * @return
     */
    public Map<String, List<ProductionTransportPrice>> findByContract(Long contractId) {
        if (StringUtils.isNull(contractId))
            return null;
        QueryWrapper<ProductionTransportPrice> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        query.orderByDesc("take_time");
        List<ProductionTransportPrice> priceList = transportPriceDao.selectList(query);
        if (priceList == null || priceList.size() == 0)
            return null;
        Map<String, List<ProductionTransportPrice>> map = new LinkedHashMap<>();
        for (int i = 0; i < priceList.size(); i++) {
            ProductionTransportPrice price = priceList.get(i);
            if (!StringUtils.isNull(price.getConIds())) {//查询是否有按工程计算的合同
                String conNames = convertConName(price.getConIds(), price.getOrgId());
                price.setConNames(conNames);
            }
            String takeTime = DateUtil.convertString(price.getTakeTime());
            if (map.containsKey(takeTime)) {
                List<ProductionTransportPrice> tempList = map.get(takeTime);
                tempList.add(price);
                map.put(takeTime, tempList);
            } else {
                List<ProductionTransportPrice> tempList = new ArrayList<>();
                tempList.add(price);
                map.put(takeTime, tempList);
            }
        }
        return map;
    }

    /**
     * 新增生产合同运输单价信息
     * @return
     */
    public RestResultDto savePrice(ProductionTransportPrice transportPrice) {
        if (transportPrice == null)
            throw new MessageCenterException("参数错误");
        if (StringUtils.isNull(transportPrice.getOrgId()))
            throw new MessageCenterException("合同公司信息为空");
        if (StringUtils.isNull(transportPrice.getContractId()))
            throw new MessageCenterException("合同信息为空");
        if (StringUtils.isNull(transportPrice.getSupplierId()))
            throw new MessageCenterException("运输商信息为空");
        if (StringUtils.isNull(transportPrice.getTransportType()))
            throw new MessageCenterException("标号运输类型信息为空");
        if (StringUtils.isNull(transportPrice.getCountType()))
            throw new MessageCenterException("计算方式为空");
        if (StringUtils.isNull(transportPrice.getCarModel()))
            throw new MessageCenterException("车辆类型为空");
        if (StringUtils.isNull(transportPrice.getRoadType()))
            throw new MessageCenterException("是否高速为空");
        if (StringUtils.isNull(transportPrice.getOverLimit()))
            throw new MessageCenterException("是否高速为空");
        if (StringUtils.isNull(transportPrice.getMinDistance()))
            throw new MessageCenterException("最小距离为空");
        if (StringUtils.isNull(transportPrice.getMaxDistance()))
            throw new MessageCenterException("最大距离为空");
        if (StringUtils.isNull(transportPrice.getMinStere()))
            throw new MessageCenterException("最小方量为空");
        if (StringUtils.isNull(transportPrice.getMaxStere()))
            throw new MessageCenterException("最大方量为空");
        if (StringUtils.isNull(transportPrice.getPrice()))
            throw new MessageCenterException("运输单价为空");
        if (StringUtils.isNull(transportPrice.getTakeTime()))
            throw new MessageCenterException("生效时间为空");

        Date nowDate = new Date();
        ProductionContractVo contract = productionContractDao.selectByIdVo(transportPrice.getContractId());
        if (contract == null || contract.getStatus().intValue() == 1)
            throw new MessageCenterException("运输合同不存在或已被删除");
        if (contract.getType().intValue() != ProductionContractTypeEnum.TRANSPORT.getCode())
            throw new MessageCenterException("运输合同类型错误");
//        ProductionTransportPrice transportPrice = new ProductionTransportPrice(orgId, contractId, supplierId, takeDate,
//                transportType, countType, carModel, isSpeed, isLimit, minDistance, maxDistance, minStere, maxStere,price);
        transportPrice.setCreateTime(nowDate);
//        transportPrice.setCreator(creator);
//        transportPrice.setRemark(remark);
//        transportPrice.setStatus(0);
        ProductionTransportPrice tempTransportPrice = transportPriceDao.findTakePrice(transportPrice);
        if (tempTransportPrice != null) {
            ProductionTransportPrice oldTransportPrice = tempTransportPrice;
            tempTransportPrice.setUpdateTime(nowDate);
            tempTransportPrice.setUpdator(transportPrice.getCreator());
            tempTransportPrice.setPrice(transportPrice.getPrice());
            tempTransportPrice.setConIds(transportPrice.getConIds());
            tempTransportPrice.setBillType(transportPrice.getBillType());
            tempTransportPrice.setPriceType(transportPrice.getPriceType());
            tempTransportPrice.setTakeTime(transportPrice.getTakeTime());
            Integer code = transportPriceDao.updateById(tempTransportPrice);
            if (code > 0) {
                try {
                    sendMessage(contract, oldTransportPrice, tempTransportPrice, 1);
                } catch (Exception e) {
                    log.error("推送消息异常");
                }
                return RestResultDto.success("保存成功", tempTransportPrice);
            }
            return RestResultDto.fail("保存运输单价异常");
        }
        Integer code = transportPriceDao.insert(transportPrice);
        if (code > 0) {
            try {
                sendMessage(contract, null, transportPrice, 0);
            } catch (Exception e) {
                log.error("推送消息异常");
            }
            return RestResultDto.success("保存成功");
        }
        return RestResultDto.fail("保存运输单价异常");
    }
    /**
     * 修改运输单价
     * @return
     */
    public RestResultDto updatePrice(ProductionTransportPrice newTransportPrice) {
        if (newTransportPrice == null || StringUtils.isNull(newTransportPrice.getId()))
            throw new MessageCenterException("参数错误");
        ProductionTransportPrice transportPrice = transportPriceDao.selectById(newTransportPrice.getId());
        if (transportPrice == null || transportPrice.getStatus() == 1)
            throw new MessageCenterException("运输单价不存在或该单价已被禁用");
//        ProductionTransportPrice newTransportPrice = new ProductionTransportPrice(transportPrice.getOrgId(), transportPrice.getContractId(),
//                transportPrice.getSupplierId(), transportPrice.getTakeTime(), transportType, countType, carModel,
//                isSpeed, isLimit, minDistance, maxDistance, minStere, maxStere,price);
        ProductionTransportPrice oldTransportPrice = transportPrice;
        if (!StringUtils.isNull(newTransportPrice.getTransportType()))
            transportPrice.setTransportType(newTransportPrice.getTransportType());
        if (!StringUtils.isNull(newTransportPrice.getCountType()))
            transportPrice.setCountType(newTransportPrice.getCountType());
        if (!StringUtils.isNull(newTransportPrice.getCarModel()))
            transportPrice.setCarModel(newTransportPrice.getCarModel());
        if (!StringUtils.isNull(newTransportPrice.getRoadType()))
            transportPrice.setRoadType(newTransportPrice.getRoadType());
        if (!StringUtils.isNull(newTransportPrice.getOverLimit()))
            transportPrice.setOverLimit(newTransportPrice.getOverLimit());
        if (!StringUtils.isNull(newTransportPrice.getMinDistance()))
            transportPrice.setMinDistance(newTransportPrice.getMinDistance());
        if (!StringUtils.isNull(newTransportPrice.getMaxDistance()))
            transportPrice.setMaxDistance(newTransportPrice.getMaxDistance());
        if (!StringUtils.isNull(newTransportPrice.getMinStere()))
            transportPrice.setMinStere(newTransportPrice.getMinStere());
        if (!StringUtils.isNull(newTransportPrice.getMaxStere()))
            transportPrice.setMaxStere(newTransportPrice.getMaxStere());
        if (!StringUtils.isNull(newTransportPrice.getPrice()))
            transportPrice.setPrice(newTransportPrice.getPrice());
        if (!StringUtils.isNull(newTransportPrice.getTakeTime())) {
            transportPrice.setTakeTime(newTransportPrice.getTakeTime());
        }
        if (!StringUtils.isNull(newTransportPrice.getConIds()))
            transportPrice.setConIds(newTransportPrice.getConIds());
        if (!StringUtils.isNull(newTransportPrice.getBillType()))
            transportPrice.setBillType(newTransportPrice.getBillType());
        if (!StringUtils.isNull(newTransportPrice.getPriceType()))
            transportPrice.setPriceType(newTransportPrice.getPriceType());
        Date nowDate = new Date();
        transportPrice.setUpdator(newTransportPrice.getUpdator());
        transportPrice.setUpdateTime(nowDate);
        Integer code = transportPriceDao.updateById(transportPrice);
        if (code > 0) {
            try {
                ProductionContractVo contract = productionContractDao.selectByIdVo(transportPrice.getContractId());
                sendMessage(contract, oldTransportPrice, newTransportPrice, 1);
            } catch (Exception e) {
                log.error("推送消息异常");
            }
            return RestResultDto.success("更新成功", transportPrice);
        }
        return RestResultDto.fail("更新运输单价异常");
    }

    /**
     * 更新运输单价工程信息
     * @param id
     * @param contractId
     * @param creator
     * @param type 0添加 1删除
     * @return
     */
    public RestResultDto relatedContract(Long id, Long contractId, String creator, Integer type) {
        if (StringUtils.isAllParamNull(id,contractId, type))
            throw new MessageCenterException("参数错误");

        ProductionTransportPrice transportPrice = transportPriceDao.selectById(id);
        if (transportPrice == null || transportPrice.getStatus() == 1)
            throw new MessageCenterException("单价信息不存在或已删除");
        String ids = transportPrice.getConIds();
        if (type == 0) {
            if (StringUtils.isNull(ids))
                ids = contractId + "";
            else
                ids += "," + contractId;
        } else {
            if (StringUtils.isNull(ids))
                throw new MessageCenterException("取消关联错误,单价工程信息不存在或已删除");
            String[] ids_ = ids.split(",");
            if (ids_ == null || ids_.length == 0)
                throw new MessageCenterException("取消关联错误,单价工程信息不存在或已删除");
            String tempIds = "";
            String contractId_ = contractId + "";
            for (int i = 0; i < ids_.length; i++) {
                String tempId = ids_[i];
                if (!tempId.equals(contractId_)) {
                    if (StringUtils.isNull(tempIds))
                        tempIds = tempId;
                    else
                        tempIds += "," + tempId;
                }
            }
            ids = tempIds;
        }
        transportPrice.setConIds(ids);
        transportPrice.setUpdator(creator);
        transportPrice.setUpdateTime(new Date());
        Integer code = transportPriceDao.updateById(transportPrice);
        if (code > 0) {
            if (!StringUtils.isNull(transportPrice.getConIds())) {
                String conNames = convertConName(transportPrice.getConIds(), transportPrice.getOrgId());
                transportPrice.setConNames(conNames);
                return RestResultDto.success("更新成功", transportPrice);
            }
            return RestResultDto.success("更新成功");
        }
        throw new MessageCenterException("更新关联信息异常");
    }

    /**
     * 禁用合同
     * @param id
     * @param status
     * @return
     */
    public RestResultDto updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isAllParamNull(id, status))
            throw new MessageCenterException("参数错误");
        ProductionTransportPrice transportPrice = transportPriceDao.selectById(id);
        if (transportPrice == null)
            throw new MessageCenterException("运输单价不存在");
        transportPrice.setStatus(status);
        transportPrice.setUpdateTime(new Date());
        transportPrice.setUpdator(creator);
        Integer code = transportPriceDao.updateById(transportPrice);
        if (code > 0) {
            return RestResultDto.success("更新成功", transportPrice);
        }
        return RestResultDto.fail("更新运输单价异常");
    }

    /**
     * 消息推送
     * @param contract
     * @param oldTransportPrice
     * @param newTransportPrice
     * @param updateType
     */
    private void sendMessage(ProductionContractVo contract, ProductionTransportPrice oldTransportPrice,
                             ProductionTransportPrice newTransportPrice, Integer updateType) {
        String touser = "lucky|1lt1556|";
        String toname = "万强|罗涛|";
        String message = "";
        Date nowDate = new Date();
        String title = "";//推送标题
        String orgName = "";
        String isSpeeds = "非高速";
        String isLimits = "不经超限站";
        String transportTypes = "普通混凝土";//运输货物类型
        String carModels = "";//车辆类型
        String countTypes = "";//计算类型
        String oldIsSpeeds = "非高速";
        String oldIsLimits = "不经超限站";
        String oldTransportTypes = "普通混凝土";//运输货物类型
        String oldCarModels = "";//车辆类型
        String oldCountTypes = "";//计算类型
        if (StringUtils.isNull(newTransportPrice.getMinDistance()))
            newTransportPrice.setMinDistance(0.0);
        if (StringUtils.isNull(newTransportPrice.getMaxDistance()))
            newTransportPrice.setMaxDistance(0.0);
        if (StringUtils.isNull(newTransportPrice.getMinStere()))
            newTransportPrice.setMinStere(0.0);
        if (StringUtils.isNull(newTransportPrice.getMaxStere()))
            newTransportPrice.setMaxStere(0.0);
        if (!StringUtils.isNull(newTransportPrice.getRoadType()))
            isSpeeds = newTransportPrice.getRoadType() == 0 ? isSpeeds : "高速";
        if (!StringUtils.isNull(newTransportPrice.getOverLimit()))
            isLimits = newTransportPrice.getOverLimit() == 0 ? isLimits : "经超限站";
        if (!StringUtils.isNull(newTransportPrice.getCarModel())) {
            for (int i = 0; i < CarModelTypeEnum.values().length; i++) {
                if (newTransportPrice.getCarModel() == CarModelTypeEnum.values()[i].getCode()) {
                    carModels = CarModelTypeEnum.values()[i].getDescription();
                    break;
                }
                if (oldTransportPrice != null && !StringUtils.isNull(oldTransportPrice.getCarModel())) {
                    if (oldTransportPrice.getCarModel() == CarModelTypeEnum.values()[i].getCode()) {
                        oldCarModels = CarModelTypeEnum.values()[i].getDescription();
                        break;
                    }
                }
            }
        }

        if (!StringUtils.isNull(newTransportPrice.getTransportType())) {
            for (int i = 0; i < TransportTypeEnum.values().length; i++) {
                if (newTransportPrice.getTransportType() == TransportTypeEnum.values()[i].getCode()) {
                    transportTypes = TransportTypeEnum.values()[i].getDescription();
                    break;
                }
                if (oldTransportPrice != null && !StringUtils.isNull(oldTransportPrice.getTransportType())) {
                    if (oldTransportPrice.getTransportType() == TransportTypeEnum.values()[i].getCode()) {
                        oldCarModels = TransportTypeEnum.values()[i].getDescription();
                        break;
                    }
                }
            }
        }
        if (!StringUtils.isNull(newTransportPrice.getCountType())) {
            for (int i = 0; i < CountBillTypeEnum.values().length; i++) {
                if (newTransportPrice.getCountType() == CountBillTypeEnum.values()[i].getCode()) {
                    countTypes = CountBillTypeEnum.values()[i].getDescription();
                    break;
                }
                if (oldTransportPrice != null && !StringUtils.isNull(oldTransportPrice.getCountType())) {
                    if (oldTransportPrice.getCountType() == CountBillTypeEnum.values()[i].getCode()) {
                        oldCountTypes = CountBillTypeEnum.values()[i].getDescription();
                        break;
                    }
                }
            }
        }

        if (!StringUtils.isNull(contract.getOrgName()))
            orgName = contract.getOrgName();
        if (updateType == 0) {
            message = "【运输合同单价新增通知】 " + orgName + " 编号:" + contract.getCode();
            message += "\n运输合同名称:" + contract.getName();
            if (!StringUtils.isNull(contract.getSupplierName()))
                message += "\n供应商:" + contract.getName();
            message += "\n道理策略:" + isSpeeds;
            message += "\n是否经超限站:" + isLimits;
            if (!StringUtils.isNull(carModels))
                message += "\n车辆类型:" + carModels;
            if (!StringUtils.isNull(countTypes))
                message += "\n计算类型:" + countTypes;

            message += "\n运输半径:" + newTransportPrice.getMinDistance() + "km ~ " + newTransportPrice.getMaxDistance() + "km";
            message += "\n方量区间:" + newTransportPrice.getMinStere() + "m³ ~ " + newTransportPrice.getMaxStere() + "m³";
            if (!StringUtils.isNull(newTransportPrice.getTakeTime()))
                message += "\n生效时间:" + DateUtil.convertString(newTransportPrice.getTakeTime());
            message += "\n运输货物:" + transportTypes + ",单价:" + newTransportPrice.getPrice() + "元。";
            if (!StringUtils.isNull(newTransportPrice.getCreator()))
                message += "\n操作人:" + newTransportPrice.getCreator();

            if (!StringUtils.isNull(newTransportPrice.getRemark()))
                message += "\n备注:" + newTransportPrice.getRemark();
            message += "\n操作时间:" + DateUtil.convertString(nowDate);
        } else if (updateType == 1){
            message = "【运输合同单价变更通知】 " + orgName + " 编号:" + contract.getCode();
            message += "\n运输合同名称:" + contract.getName();
            if (!StringUtils.isNull(contract.getSupplierName()))
                message += "\n供应商:" + contract.getName();
            if (!StringUtils.isNull(oldTransportPrice.getRoadType()) &&!StringUtils.isNull(newTransportPrice.getRoadType())
                    && oldTransportPrice.getRoadType() != newTransportPrice.getRoadType()) {
                oldIsSpeeds = oldTransportPrice.getRoadType() == 0 ? "非高速" : "高速";
                message += "\n道理策略:" + oldIsSpeeds + "-->" + isSpeeds;
            }
            if (!StringUtils.isNull(oldTransportPrice.getOverLimit()) &&!StringUtils.isNull(newTransportPrice.getOverLimit())
                    && oldTransportPrice.getOverLimit() != newTransportPrice.getOverLimit()) {
                oldIsLimits = oldTransportPrice.getOverLimit() == 0 ? oldIsLimits : "经超限站";
                message += "\n是否经超限站:" + oldIsLimits + "-->" + isLimits;
            }
            if (!StringUtils.isNull(oldTransportPrice.getCarModel()) &&!StringUtils.isNull(newTransportPrice.getCarModel())
                    && oldTransportPrice.getCarModel() != newTransportPrice.getCarModel()) {
                message += "\n车辆类型:" + oldCarModels + "-->" + carModels;
            }
            if (!StringUtils.isNull(oldTransportPrice.getCountType()) &&!StringUtils.isNull(newTransportPrice.getCountType())
                    && oldTransportPrice.getCountType() != newTransportPrice.getCountType()) {
                message += "\n计算类型:" + oldCountTypes + "-->" + countTypes;
            }
            if (!StringUtils.isNull(oldTransportPrice.getMinDistance()) &&!StringUtils.isNull(newTransportPrice.getMinDistance())
                    && oldTransportPrice.getMinDistance() != newTransportPrice.getMinDistance()
                    && !StringUtils.isNull(oldTransportPrice.getMaxDistance()) &&!StringUtils.isNull(newTransportPrice.getMaxDistance())
                    && oldTransportPrice.getMaxDistance() != newTransportPrice.getMaxDistance()) {
                message += "\n运输半径:" + oldTransportPrice.getMinDistance() + "-->"
                        + newTransportPrice.getMinDistance() + "km ~ " + oldTransportPrice.getMaxDistance() + "-->"
                        + newTransportPrice.getMaxDistance() + "km";
            }
            if (!StringUtils.isNull(oldTransportPrice.getMinStere()) &&!StringUtils.isNull(newTransportPrice.getMinStere())
                    && oldTransportPrice.getMinStere() != newTransportPrice.getMinStere()
                    && !StringUtils.isNull(oldTransportPrice.getMaxStere()) &&!StringUtils.isNull(newTransportPrice.getMaxStere())
                    && oldTransportPrice.getMaxStere() != newTransportPrice.getMaxStere()) {
                message += "\n方量区间:" + oldTransportPrice.getMinStere() + "-->"
                        + newTransportPrice.getMinStere() + "km ~ " + oldTransportPrice.getMaxStere() + "-->"
                        + newTransportPrice.getMaxStere() + "km";
            }
            if (!StringUtils.isNull(transportTypes) && !StringUtils.isNull(transportTypes) && !transportTypes.equals(oldTransportTypes))
                message += "\n运输货物:" + oldTransportTypes + "-->" + transportTypes + "。";
            if (!StringUtils.isNull(newTransportPrice.getPrice()) && !StringUtils.isNull(oldTransportPrice.getPrice()))
                message += "\n单价:" + oldTransportPrice.getPrice() + "-->" + newTransportPrice.getPrice() + "元。";
            if (!StringUtils.isNull(newTransportPrice.getCreator()))
                message += "\n操作人:" + newTransportPrice.getCreator();

            if (!StringUtils.isNull(newTransportPrice.getRemark()))
                message += "\n备注:" + newTransportPrice.getRemark();
            message += "\n操作时间:" + DateUtil.convertString(nowDate);
        }

        List<Map<String, Object>> list = userPositionDao.hasGeneralManager(contract.getOrgId(), "财务统计", "统计");
        List<Map<String, Object>> list1 = userPositionDao.hasGeneralManager(contract.getOrgId(), "生产内业", "生产统计");
        List<Map<String, Object>> list2 = userPositionDao.hasGeneralManager(contract.getOrgId(), "生产副总", "生产经理");
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> temp = list.get(i);
                String name = StringUtils.convertString(temp.get("name"));
                if (!StringUtils.isNull(name) && toname.indexOf(name) == -1) {
                    toname += temp.get("name") + "|";
                    touser += temp.get("username") + "|";
                }
            }
        }
        if (list1 != null && list1.size() > 0) {
            for (int i = 0; i < list1.size(); i++) {
                Map<String, Object> temp = list1.get(i);
                String name = StringUtils.convertString(temp.get("name"));
                if (!StringUtils.isNull(name) && toname.indexOf(name) == -1) {
                    toname += temp.get("name") + "|";
                    touser += temp.get("username") + "|";
                }
            }
        }
        if (list2 != null && list2.size() > 0) {
            for (int i = 0; i < list2.size(); i++) {
                Map<String, Object> temp = list2.get(i);
                String name = StringUtils.convertString(temp.get("name"));
                if (!StringUtils.isNull(name) && toname.indexOf(name) == -1) {
                    toname += temp.get("name") + "|";
                    touser += temp.get("username") + "|";
                }
            }
        }

        toname = toname.substring(0, toname.length() - 1);
        touser = touser.substring(0, touser.length() - 1);
        SysMessageRecord record = new SysMessageRecord(touser, toname, "", WechatOaGroupApi.corpID, "", contract.getOrgId(),
                "", 1, 1, title, "",
                message, "", 1, 0, "", "", nowDate);
        messageRecordDao.insert(record);
        WechatOaGroupApi.sendTextMessage(WechatOaGroupApi.corpID, WechatOaGroupApi.corpSecret,
                WechatOaGroupApi.agentId, touser, message);

    }

	@Override
	public ProductionTransportPrice queryTranPrice(Long tranConId, Integer carModel, Integer transportType, Integer countType, Integer roadType, Integer overLimit, Double distance, String tranTime) {
		if (tranConId == null || distance == null || StringUtils.isBlank(tranTime)) {
			//	没有合同数据
			return null;
		}
    	/*QueryWrapper<ProductionTransportPrice> qw = new QueryWrapper<>();
    	qw.eq("status", StateEnum.ENABLED.getCode());
    	qw.eq("car_model",carModel);
    	qw.eq("transport_Type",transportType);
    	qw.eq("road_type",roadType);
    	qw.eq("over_limit",overLimit);
		qw.le("start_time",tranTime);
		qw.and(e ->{
			//	截止时间大于运输时间
			e.isNull("end_time").or().gt("end_time",tranTime);
		});*/
		ProductionTransportPrice transportPrice = transportPriceDao.queryTranPrice(tranConId, carModel, transportType, countType, roadType, overLimit, distance, tranTime);
		return transportPrice;
	}

	@Override
	public ProductionTransportPrice queryIncrePrice(Long tranConId, Integer carModel, Integer invTranType, Integer invCountType, Integer roadType, Integer overLimit, String tranTime) {
		ProductionTransportPrice transportPrice = transportPriceDao.queryIncrePrice(tranConId, carModel, invTranType, invCountType, roadType, overLimit, tranTime);
		return transportPrice;
	}

	/**
     * 转换查询运输单价中含工程合同的信息
     * @param conIds
     * @param orgId
     * @return
     */
	private String convertConName(String conIds, Long orgId) {
        String[] ids = conIds.split(",");
        String conNames = "";
        if (ids != null && ids.length > 0) {
            SaleContract contract = new SaleContract();
            String conIds_ = "";
            for (int j = 0; j < ids.length; j++) {
                if (j == 0) {
                    conIds_ = ids[j];
                } else {
                    conIds_ += "," + ids[j];
                }
            }
            contract.setConIds(conIds_);
            contract.setStatus(0);
            contract.setOrgId(orgId);
            List<SaleContractVo> contractList = saleContractDao.findContractVo(contract);
            if (contractList != null && contractList.size() > 0) {
                Map<Integer, String> categoryMap = new HashMap<>();
                for (int i = 0; i < ContractCategoryEnum.values().length; i++) {
                    categoryMap.put(ContractCategoryEnum.values()[i].getCode(), ContractCategoryEnum.values()[i].getDescription());
                }
                for (int i = 0; i < contractList.size(); i++) {
                    SaleContract saleContract = contractList.get(i);
                    String contractCategory = StringUtils.convertString(categoryMap.get(saleContract.getContractCategory()));
                    if (!StringUtils.isNull(saleContract.getBatchId())) {
                        if (saleContract.getBatchId().equals(saleContract.getId())) {
                            contractCategory += "[主工程]";
                        } else {
                            contractCategory += "[主工程]";
                        }
                    } else {
                        contractCategory += "[主工程]";
                    }
                    if (i == 0) {
                        conNames = saleContract.getName() + "{" + contractCategory + "}";
                    } else {
                        conNames += "," + saleContract.getName() + "{" + contractCategory + "}";
                    }
                }
            }
        }
        return conNames;
    }
}